package com.mujin.keji.collectionsystem.module.protectionrules.view;

import android.arch.lifecycle.ViewModelProviders;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mujin.keji.collectionsystem.R;
import com.mujin.keji.collectionsystem.base.BaseActivity;
import com.mujin.keji.collectionsystem.db.database.AppDataBase;
import com.mujin.keji.collectionsystem.db.domain.VisitRuleBean;
import com.mujin.keji.collectionsystem.module.protectionrules.viewmodel.OutRuleViewModel;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 外访守则
 *
 * @author weishixiong
 * @Time 2018-05-17
 */
public class OutRuleActivity extends BaseActivity {
    private TextView tvContent;
    private OutRuleViewModel outRuleViewModel;
    private TextView tvMsgTitle;
    private SmartRefreshLayout srHasData;
    private SmartRefreshLayout srNoData;
    private TextView tvNoData;
    private LinearLayout llNoData;
    private List<VisitRuleBean> rules;



    @Override
    protected void createViewModel() {
        outRuleViewModel = ViewModelProviders.of(this).get(OutRuleViewModel.class);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_outrule;
    }

    @Override
    public void initView() {
        setCenterTitle("外访守则", "#FFFFFF");
        setleftImage(R.drawable.image_arrow_left_white, true, null);
        tvContent = (TextView) findViewById(R.id.tv_content);
        tvMsgTitle = (TextView) findViewById(R.id.tv_msg_title);
        srHasData = (SmartRefreshLayout) findViewById(R.id.sr_has_data);
        srNoData = (SmartRefreshLayout) findViewById(R.id.sr_no_data);
        tvNoData = (TextView) findViewById(R.id.tv_no_data);
        llNoData = (LinearLayout) findViewById(R.id.ll_no_data);
        srHasData.setEnableLoadMore(false);
        srNoData.setEnableLoadMore(false);
    }

    @Override
    public void initData() {
        getLoadingDialog().show();
        loadData();

    }

    /**
     * 获取外访守则数据
     */
    private void loadData(){
        //初始化一条数据
        new Thread() {
            @Override
            public void run() {
                super.run();
                VisitRuleBean visitRuleBean = new VisitRuleBean();
                visitRuleBean.setTitle("test");
                visitRuleBean.setContent("testtesttesttesttesttesttesttesttesttesttesttesttest/n");
                AppDataBase.getInstance(OutRuleActivity.this).getVisitRuleDao().addVisitRule(visitRuleBean);
            }
        }.start();


        Flowable<List<VisitRuleBean>> flowable = AppDataBase.getInstance(this).getVisitRuleDao().queryVisitRule();

        flowable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<VisitRuleBean>>(){
                    @Override
                    public void onSubscribe(Subscription s) {
                        s.request(1);
                    }

                    @Override
                    public void onNext(List<VisitRuleBean> visitRuleBeans) {
                        srHasData.setVisibility(View.VISIBLE);
                        llNoData.setVisibility(View.GONE);
                        VisitRuleBean visitRuleBean = visitRuleBeans.get(0);
                        tvMsgTitle.setText(visitRuleBean.getTitle());
                        tvContent.setText(visitRuleBean.getContent());
                        getLoadingDialog().dismiss();
                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });


//        outRuleViewModel.getLiveData().observe(OutRuleActivity.this, new Observer<BaseDto<OutRuleDto>>() {
//            @Override
//            public void onChanged(@Nullable BaseDto<OutRuleDto> outRuleDtoBaseDto) {
//                getLoadingDialog().dismiss();
//                srHasData.finishRefresh();
//                srNoData.finishRefresh();
//                if (EmptyUtils.isNotEmpty(outRuleDtoBaseDto.getData())) {
//                    srHasData.setVisibility(View.VISIBLE);
//                    llNoData.setVisibility(View.GONE);
//                    tvMsgTitle.setText(outRuleDtoBaseDto.getData().getTitle());
//                    tvContent.setText(outRuleDtoBaseDto.getData().getContent());
//                } else {
//                    if (outRuleDtoBaseDto.getCode() != Constant.Server.SUCCESS_CODE) {
//                        ToastUtils.showToast(outRuleDtoBaseDto.getMsg());
//                    }
//                    srHasData.setVisibility(View.GONE);
//                    llNoData.setVisibility(View.VISIBLE);
//                    tvNoData.setText("暂无外访守则");
//                }
//            }
//        });
    }
    @Override
    public void initListener() {
        srHasData.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                loadData();
            }
        });
        srNoData.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                loadData();
            }
        });
    }
}
